package net.blank.ui.core
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.events.TouchEvent;
	import flash.utils.Dictionary;
	
	/**
	 * 派生自 InteractiveUI 对象的 enabled 属性被设置为 true (启用交互事件)时派发
	 */
	[Event(name="enabled", type="ui.core.InteractiveUIEvent")]
	
	/**
	 * 派生自 InteractiveUI 对象的 enabled 属性被设置为 false (禁用交互事件)时派发
	 */
	[Event(name="disabled", type="ui.core.InteractiveUIEvent")]
	
	/**
	 * 可接受交互事件显示对象
	 * @author blank
	 * 
	 */
	public class InteractiveUI extends AbstractRenderOptUI
	{
		/**
		 * 交互性事件列表
		 */
		private static const INTERACTIVE_EVENT_LIST:Dictionary=new Dictionary();
		
		/**
		 * 初始化交互性事件列表
		 * 
		 */
		PLATFORM::PC{
			private static function initInteractiveEventList():void{
				INTERACTIVE_EVENT_LIST[MouseEvent.CLICK]=true;
				
				INTERACTIVE_EVENT_LIST[MouseEvent.MOUSE_DOWN]=true;
				INTERACTIVE_EVENT_LIST[MouseEvent.MOUSE_UP]=true;
				
				INTERACTIVE_EVENT_LIST[MouseEvent.MOUSE_MOVE]=true;
				
				INTERACTIVE_EVENT_LIST[MouseEvent.MOUSE_OUT]=true;
				INTERACTIVE_EVENT_LIST[MouseEvent.MOUSE_OVER]=true;
				
				INTERACTIVE_EVENT_LIST[MouseEvent.ROLL_OUT]=true;
				INTERACTIVE_EVENT_LIST[MouseEvent.ROLL_OVER]=true;
				
				INTERACTIVE_EVENT_LIST[KeyboardEvent.KEY_DOWN]=true;
				INTERACTIVE_EVENT_LIST[KeyboardEvent.KEY_UP]=true;
				
				INTERACTIVE_EVENT_LIST[MouseEvent.MOUSE_WHEEL]=true;
				
				INTERACTIVE_EVENT_LIST[TextEvent.TEXT_INPUT]=true;
			}
		}
		PLATFORM::MOBILE{
			private static function initInteractiveEventList():void{
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_TAP]=true;
				
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_BEGIN]=true;
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_END]=true;
				
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_MOVE]=true;
				
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_OUT]=true;
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_OVER]=true;
				
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_ROLL_OUT]=true;
				INTERACTIVE_EVENT_LIST[TouchEvent.TOUCH_ROLL_OVER]=true;
				
				INTERACTIVE_EVENT_LIST[KeyboardEvent.KEY_DOWN]=true;
				INTERACTIVE_EVENT_LIST[KeyboardEvent.KEY_UP]=true;
				
				INTERACTIVE_EVENT_LIST[TextEvent.TEXT_INPUT]=true;
			}
		}
		
		initInteractiveEventList();
		
		/**
		 * 侦听器列表
		 */
		private var ivtEls:Vector.<EventListener>=new Vector.<EventListener>();
		
		/**
		 * 是否可接受交互事件
		 */
		private var _enabled:Boolean=true;
		
		/**
		 * 是否可接受交互事件
		 * @param value
		 * 
		 */
		public function set enabled(value:Boolean):void{
			if(value != _enabled){
				_enabled=value;
				
				var el:EventListener;
				if(_enabled){
					for each(el in ivtEls){
						super.addEventListener(el.type,el.listener,el.useCapture,el.priority,el.useWeakReference);
					}
					mouseChildren=true;
					dispatchEvent(new InteractiveUIEvent(InteractiveUIEvent.ENABLED));
				}else{
					for each(el in ivtEls){
						super.removeEventListener(el.type,el.listener,el.useCapture);
					}
					mouseChildren=false;
					dispatchEvent(new InteractiveUIEvent(InteractiveUIEvent.DISABLED));
				}
			}
		}
		public function get enabled():Boolean{
			return _enabled;
		}
		
		/**
		 * 忽略 enabled 属性状态对交互性事件的控制(如tip可能要求在 enabled 为 false 时也能触发鼠标进入与离开事件)添加事件
		 * @param type
		 * @param listener
		 * @param useCapture
		 * @param priority
		 * @param useWeakReference
		 * 
		 */
		public function addEventListenerIgnoreEnabledState(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{
			if(type in INTERACTIVE_EVENT_LIST){
				for each(var el:EventListener in ivtEls){
					if(el.type == type && el.listener == listener && el.useCapture == useCapture){
						return;
					}
				}
				ivtEls[ivtEls.length]=new EventListener(type,listener,useCapture,priority,useWeakReference);
				if(_enabled){
					super.addEventListener(type,listener,useCapture,priority,useWeakReference);
				}
			}else{
				super.addEventListener(type,listener,useCapture,priority,useWeakReference);
			}
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
			if(type in INTERACTIVE_EVENT_LIST){
				for(var i:int=ivtEls.length-1; i > -1; i--){
					var el:EventListener=ivtEls[i];
					if(el.type == type && el.listener == listener && el.useCapture == useCapture){
						ivtEls.splice(i,1);
						break;
					}
				}
			}
			super.removeEventListener(type,listener,useCapture);
		}
		
		/**
		 * 可接受交互事件显示对象
		 * 
		 */
		public function InteractiveUI(){
			super();
		}
		
		override public function destroy():void{
			ivtEls=null;
			
			super.destroy();
		}
	}
}